﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StoragePeriod.IDAL.FoodTesting;
using StoragePeriod.Model.FoodTesting;
using System.Data;
using StoragePeriod.DBUtility;
using System.Data.SqlClient;

namespace StoragePeriod.SqlServerDAL.FoodTesting
{
    /// <summary>
    /// 蛋白质含量表 接口
    /// </summary>
    public class D_ProteinData : I_ProteinData
    {
        /// <summary>
        /// 获取检查数据实体
        /// </summary>
        /// <param name="eVcData">查询ID</param>
        /// <returns>返回对应的实体</returns>
        public E_ProteinData GetDataEntityById(E_ProteinData eProteinData)
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("select * from ProteinData where id=" + eProteinData.id);
            DataTable dt = DbHelperSQL.Query(sbSql.ToString()).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                E_ProteinData Entity = new ModelHandler<E_ProteinData>().FillModel(dt.Rows[0]);
                return Entity;
            }
            return null;
        }

        /// <summary>
        /// 根据原料ID和检测时间 删除检测记录
        /// </summary>
        /// <param name="eVcData">要删除的检测记录</param>
        /// <returns></returns>
        public int RemoveByTime(E_ProteinData eProteinData)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ProteinData where RawId=" + eProteinData.RawId + " and ProteinTime=CAST('" + eProteinData.ProteinTime.ToString() + "' as datetime)");
            return DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除某一次检测记录
        /// </summary>
        /// <param name="eVcData">要删除的检测记录</param>
        /// <returns></returns>
        public int RemoveByFoodNum(E_ProteinData eProteinData)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ProteinData where RawId=" + eProteinData.RawId + " and ProteinTime=CAST('" + eProteinData.ProteinTime.ToString() + "' as datetime) and FoodNum=" + eProteinData.FoodNum);
            return DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 根据原料ID 和检测时间 获取所有的检测记录
        /// </summary>
        /// <param name="eVcData">要获取的检测记录</param>
        /// <returns></returns>
        public List<E_ProteinData> GetDataListByTime(E_ProteinData eProteinData)
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("select * from ProteinData where RawId=" + eProteinData.RawId + " and ProteinTime=cast('" + eProteinData.ProteinTime.ToString() + "' as datetime)");
            DataTable dt = DbHelperSQL.Query(sbSql.ToString()).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                List<E_ProteinData> EntityList = new ModelHandler<E_ProteinData>().FillModel(dt);
                return EntityList;
            }
            return null;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(E_ProteinData eProteinData)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ProteinData(");
            strSql.Append("RawId,RawName,Protein_m,Protein_X,Protein_PJ,Protein_JD,Protein_JMD,Protein_V1,Protein_V2,Protein_V3,Protein_C,Protein_F,ProteinTime,FoodNum)");
            strSql.Append(" values (");
            strSql.Append("@RawId,@RawName,@Protein_m,@Protein_X,@Protein_PJ,@Protein_JD,@Protein_JMD,@Protein_V1,@Protein_V2,@Protein_V3,@Protein_C,@Protein_F,@ProteinTime,@FoodNum)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@RawId", SqlDbType.Int,4),
					new SqlParameter("@RawName", SqlDbType.VarChar,50),
					new SqlParameter("@Protein_m", SqlDbType.VarChar,50),
					new SqlParameter("@Protein_X", SqlDbType.VarChar,50),
					new SqlParameter("@Protein_PJ", SqlDbType.VarChar,50),
					new SqlParameter("@Protein_JD", SqlDbType.VarChar,50),
					new SqlParameter("@Protein_JMD", SqlDbType.VarChar,50),
					new SqlParameter("@Protein_V1", SqlDbType.VarChar,50),
					new SqlParameter("@Protein_V2", SqlDbType.VarChar,50),
					new SqlParameter("@Protein_V3", SqlDbType.VarChar,50),
					new SqlParameter("@Protein_C", SqlDbType.VarChar,50),
					new SqlParameter("@Protein_F", SqlDbType.VarChar,50),
					new SqlParameter("@ProteinTime", SqlDbType.DateTime),
					new SqlParameter("@FoodNum", SqlDbType.Int,4)};
            parameters[0].Value = eProteinData.RawId;
            parameters[1].Value = eProteinData.RawName;
            parameters[2].Value = eProteinData.Protein_m;
            parameters[3].Value = eProteinData.Protein_X;
            parameters[4].Value = eProteinData.Protein_PJ;
            parameters[5].Value = eProteinData.Protein_JD;
            parameters[6].Value = eProteinData.Protein_JMD;
            parameters[7].Value = eProteinData.Protein_V1;
            parameters[8].Value = eProteinData.Protein_V2;
            parameters[9].Value = eProteinData.Protein_V3;
            parameters[10].Value = eProteinData.Protein_C;
            parameters[11].Value = eProteinData.Protein_F;
            parameters[12].Value = eProteinData.ProteinTime;
            parameters[13].Value = eProteinData.FoodNum;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
    }
}
